package com.zrrd.blog.system.controller;


import com.zrrd.blog.api.entities.SysUser;
import com.zrrd.blog.system.req.SysUserCheckPasswordREQ;
import com.zrrd.blog.system.req.SysUserREQ;
import com.zrrd.blog.system.req.SysUserUpdatePasswordREQ;
import com.zrrd.blog.system.service.ISysUserService;
import com.zrrd.blog.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2023-08-11
 */
@Api(value="用户接口管理",description = "用户接口管理，包含增删改查")
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation("根据用户名和手机号查询用户列表")
    @PostMapping("/search")
    public Result search(@RequestBody SysUserREQ req){
        return sysUserService.queryPage(req);
    }

    @ApiOperation("根据用户id查询所有角色ids")
    @ApiImplicitParam(name="id",value = "用户id",required = true)
    @GetMapping("/{id}/role/ids")
    public Result findRoleIdsById(@PathVariable("id") String id){
        return sysUserService.findRoleIdsById(id);
    }

    @ApiOperation("新增用户角色接口")
    @PostMapping("/{id}/role/save")
    public Result saveUserRole(@PathVariable("id") String id,@RequestBody List<String> roleIds){
        return sysUserService.saveUserRole(id,roleIds);
    }

    @ApiOperation("删除用户信息接口")
    @ApiImplicitParam(name="id",value = "用户id",required = true)
    @DeleteMapping("/{id}")
    public Result deleteUserRoleByUserId(@PathVariable("id") String id){
        return sysUserService.deleteUserRoleByUserId(id);
    }

    @ApiOperation("新增用户详情")
    @PostMapping
    public Result save(@RequestBody SysUser sysUser){
        String password=passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(password);
        sysUserService.save(sysUser);
        return Result.ok();
    }

    @ApiOperation("根据id查询用户详情")
    @ApiImplicitParam(name="id",value = "用户id",required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.ok(sysUserService.getById(id));
    }

    @ApiOperation("检验原密码")
    @PostMapping("/check/password")
    public Result checkPassword(@RequestBody SysUserCheckPasswordREQ req){
        return sysUserService.checkPassword(req);
    }

    @ApiOperation("更新用户密码")
    @PostMapping("/password")
    public Result updatePassword(@RequestBody SysUserUpdatePasswordREQ req){
        return sysUserService.updatePassword(req);
    }

    @ApiOperation("更新用户信息")
    @PutMapping
    public Result update(@RequestBody SysUser sysUser){
        return sysUserService.update(sysUser);
    }


}